<template>
	<view class="mainbgc">
		<header-top :messageInfo="{typeFlag:true,message:'输入支付金额',liner:false}"></header-top>
		<!-- 合计 -->
		<view class="mainpadding flexleft">
			<view class="huisetext" style="margin-right: 50rpx;">合计：{{msg.heji}}元</view>
			<view class="huisetext">预收款：{{msg.yushoukuan}}元</view>
		</view>
		<!-- 优惠和欠款 -->
		<view class="mainpadding ffffff"  v-if="msg.type!=3">
			<view class="itemscreen flexleft">
				<view class="strongtext minwidth">优惠</view>
				<view class="rightscreen flexbetween">
					<input type="text" placeholder="输入优惠金额" v-model="preferential_money">
				</view>
			</view>
			<view class="itemscreen flexleft margin_top">
				<view class="strongtext minwidth">欠款</view>
				<view class="rightscreen flexbetween">
					<input type="text" placeholder="输入欠款金额" v-model="arrears">
				</view>
			</view>
		</view>
		<!-- 更多支付方式 -->
		<view class="mainpadding flexbetween">
			<view class="huisetext" v-if="msg.type!=3">以下支付方式最多允许使用两种</view>
			<view class="lansetext" v-if="msg.type==3" @click="getpayway">还原默认支付方式 </view>
			<view class="flexleft" @click="zhiffs">
				<view class="lansetext" v-if="msg.type!=3">更多支付方式</view>
				<view class="lansetext" v-if="msg.type==3">更换支付方式</view>
				<view class="lanseytb"></view>
			</view>
		</view>
		<!-- 支付方式 -->
		<view class="mainpadding ffffff">
			<view class="itemscreen flexleft">
				<view class="strongtext minwidth">{{defaultWay.account_name}}</view>
				<view class="rightscreen flexbetween">
					<input type="text" placeholder="输入金额" v-model="moneyone">
				</view>
			</view>
			<view class="itemscreen flexleft margin_top" v-if="selectWay.id">
				<view class="strongtext minwidth">{{selectWay.account_name}}</view>
				<view class="rightscreen flexbetween">
					<input type="text" placeholder="输入金额" v-model="moneytwo">
				</view>
			</view>
		</view>
		<view class="mainpadding flexright huisetext" v-if="selectWay.money">预收款余额：{{selectWay.money}}</view>
		<view class="yesbtn" @click="submit">确认</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				msg: {
					type:1,//1销货单，2退货单，3采购单
					heji: "", //合计
					yushoukuan: "",
					purchase_code: "",
				},
				customers_id:"",
				preferential_money:"",//优惠金额
				arrears:"",//欠款
				moneyone:"",//
				moneytwo:"",//
				defaultWay: {
					id: 0,
					account_name:"",
				},
				selectWay:{
					id: 0,
					account_name:"",
				}
			}
		},
		onLoad(options) {
			let data = JSON.parse(decodeURIComponent(options.msg));
			this.msg = data
			if(data.customers_id){
				this.customers_id = data.customers_id
			}
			console.log(data,888)
			this.getpayway()
		},
		onShow() {
			if(this.msg.type==3){
				uni.$off('getselectWay')
				uni.$on('getselectWay',data=>{
					this.defaultWay = data
				})
			}else{
				uni.$off('getselectWay')
				uni.$on('getselectWay',data=>{
					this.selectWay = data
				})
			}
		},
		methods: {
			getpayway() {
				httpRequest.request('appapi/manage/index/accounts', 'POST', {
					is_default: 1 //是否默认值
				}, false, false, true).then(res => {
					this.defaultWay = res.data[0]
				}).catch(err => {
					// httpRequest.toast(err.msg);
				})
			},
			zhiffs() {
				uni.navigateTo({
					url: '/pages/workbench/caigou/xzzhifufangshi?customers_id='+this.customers_id
				})
			},
			submit(){
				let paymsg = []
				if(this.moneyone!=0 && this.moneyone!=''){
					paymsg.push(
						{
							accounts_id:this.defaultWay.id,
							money:this.moneyone
						}
					)
				}
				if(this.moneytwo!=0 && this.moneytwo!=''){
					paymsg.push(
						{
							accounts_id:this.selectWay.id,
							money:this.moneytwo
						}
					)
				}
				let url = ""
				let data = {
					purchase_code:this.msg.purchase_code,
					preferential_money:this.preferential_money==''?0:this.preferential_money,
					arrears:this.arrears==''?0:this.arrears,
					pay_data:paymsg
				}
				if(this.msg.type==1){
					url = 'appapi/manage/goods/sales_add'
				}else if(this.msg.type==2){
					url = 'appapi/manage/goods/sales_exchange_add'
					data.retreat_data = data.pay_data
					delete data.pay_data
				}else if (this.msg.type==3){
					url = 'appapi/manage/goods/purchase_add'
					data.pay_money = this.moneyone
					data.account = this.defaultWay.id
					delete data.preferential_money
					delete data.pay_data
				}else if(this.msg.type==4){
					url = 'appapi/manage/goods/order_add'
				}else if(this.msg.type==10){
					url = 'appapi/manage/goods/exchange_add'
				}
				httpRequest.request(url, 'POST', data , false, false, true).then(res => {
					httpRequest.toast(res.msg)
					if(res.code==1){
						setTimeout(function(){
							uni.navigateBack({
								delta:1
							})
						},1000)
					}
				}).catch(err => {
					// httpRequest.toast(err.msg);
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.lanseytb {
		background-image: url('@/static/image/system/lanseyoutubiao.png');
		width: 12rpx;
		height: 14rpx;
		margin-left: 10rpx;
		background-size: 100% 100%;
	}

	.lansetext {
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #0BBDCB;
	}

	.huisetext {
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #666666;
	}

	.minwidth {
		min-width: 150rpx;
	}

	.rightscreen {
		width: 100%;
	}

	.itemscreen {
		padding-bottom: 20rpx;
	}

	.yesbtn {
		// margin: 30rpx;
		border-radius: 40rpx;
		background-color: #00B855;
		color: #fff;
		padding: 20rpx 30rpx;
		text-align: center;
		position: fixed;
		bottom: 30rpx;
		width: calc(100vw - 120rpx);
	}
</style>
